"use strict";
cc._RF.push(module, 'daaf9JP2ipLF6+MfHR8pVGV', 'DragBase');
// scripts/Game/Base/DragBase.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var data_control_1 = require("../../engines/libs/data_control");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property, menu = _a.menu, disallowMultiple = _a.disallowMultiple;
var DragBase = /** @class */ (function (_super) {
    __extends(DragBase, _super);
    function DragBase() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.checkTime = 1;
        _this.origin = null;
        _this.nodePos = null;
        _this.oldPos = null;
        _this.newPos = null;
        _this.limit = { minX: 0, maxX: 0, minY: 0, maxY: 0 };
        _this.ctrlNode = null;
        _this.canMove = true;
        return _this;
    }
    Object.defineProperty(DragBase.prototype, "Limit", {
        get: function () {
            return this.limit;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(DragBase.prototype, "NodePos", {
        set: function (value) {
            this.nodePos.x = value.x;
            this.nodePos.y = value.y;
            //console.log("变换了位置");
        },
        enumerable: false,
        configurable: true
    });
    DragBase.prototype.CanMove = function (value) {
        this.canMove = value;
    };
    DragBase.prototype.start = function () {
        var _this = this;
        if (null == this.ctrlNode) {
            this.ctrlNode = this.node;
        }
        if (this.ctrlNode) {
            this.scheduleOnce(this.SetPos);
        }
        this.scheduleOnce(function () {
            var offset = data_control_1.default.GetWorldPos(_this.ctrlNode.parent);
            //console.log("偏移:",offset.x,offset.y);
            _this.limit.maxX = (cc.winSize.width * 0.5 / _this.ctrlNode.parent.scaleX) - offset.x;
            _this.limit.minX = (-cc.winSize.width * 0.5 / _this.ctrlNode.parent.scaleX) - offset.x;
            _this.limit.maxY = (cc.winSize.height * 0.5 / _this.ctrlNode.parent.scaleY) - offset.y;
            _this.limit.minY = (-cc.winSize.height * 0.5 / _this.ctrlNode.parent.scaleY) - offset.y;
        }, .1);
        if (this.ctrlNode) {
            this.RegistEvent();
        }
    };
    DragBase.prototype.SetPos = function () {
        this.origin = this.ctrlNode.getPosition();
        this.nodePos = this.ctrlNode.getPosition();
    };
    DragBase.prototype.RegistEvent = function () {
        this.ctrlNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    };
    DragBase.prototype.onDestroy = function () {
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    };
    DragBase.prototype.onTouchStart = function () {
        //     cc.tween(this.charSprite).stop();
        //     cc.tween(this.charSprite).to(.1,{scale:1},{easing:'quadIn'}).start();
        //     this.targetFlag = true;
        //     this.inBroad = true;
        //     this.node.width = this.charSprite.width;
        //     this.node.height = this.charSprite.height;
    };
    //触摸移动；
    DragBase.prototype.onTouchMove = function (event) {
        // console.log("????");
        if (!this.canMove) {
            return;
        }
        var self = this;
        var touches = event.getTouches();
        //触摸刚开始的位置
        this.oldPos = self.ctrlNode.parent.convertToNodeSpaceAR(touches[0].getStartLocation());
        //触摸时不断变更的位置
        this.newPos = self.ctrlNode.parent.convertToNodeSpaceAR(touches[0].getLocation());
        var subPos = this.oldPos.sub(this.newPos);
        self.ctrlNode.x = self.nodePos.x - subPos.x;
        self.ctrlNode.y = self.nodePos.y - subPos.y;
        // 控制节点移不出屏幕; 
        var nPos = self.ctrlNode.getPosition(); //节点实时坐标；
        if (nPos.x < this.limit.minX) {
            nPos.x = this.limit.minX;
        }
        ;
        if (nPos.x > this.limit.maxX) {
            nPos.x = this.limit.maxX;
        }
        ;
        if (nPos.y < this.limit.minY) {
            nPos.y = this.limit.minY;
        }
        ;
        if (nPos.y > this.limit.maxY) {
            nPos.y = this.limit.maxY;
        }
        ;
        self.ctrlNode.setPosition(nPos);
    };
    DragBase.prototype.onTouchEnd = function (e) {
        if (!this.canMove) {
            return;
        }
        // this.ctrlNode.setPosition(this.origin);
        this.ctrlNode.getPosition(this.nodePos); //获取触摸结束之后的node坐标；
    };
    DragBase = __decorate([
        ccclass,
        menu('工具组件/可拖动物')
    ], DragBase);
    return DragBase;
}(cc.Component));
exports.default = DragBase;

cc._RF.pop();